#include "mainform.h"
#include "baseform.h"
#include "ui_baseform.h"

#include "tableview.h"
#include "itemdelegate.h"
#include <QStandardItemModel>
#include <QSettings>

baseForm::baseForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::baseForm)
{
    ui->setupUi(this);

    /***********table**********/

    baseTable = new TableView(380,280,base_length);
    tableInit();//表格初始化
    ui->stackedWidget->addWidget(baseTable);//添加表格
    settings = new QSettings("frp","base");
    if(settings->value("baseSetup").toInt() == 1)
        recData();
}

baseForm::~baseForm(){

    delete ui;
    saveData();
    settings->setValue("baseSetup",1);
}



void baseForm::on_addPushButton_clicked(){
    QModelIndex index;
    baseTable->model->insertRow(baseTable->currentIndex().row()+1); //加一行

    index = baseTable->model->index(baseTable->currentIndex().row()+1,base_sw, QModelIndex()); //获取index
    baseTable->model->setData(index,"true");//设置初始值

    index = baseTable->model->index(baseTable->currentIndex().row()+1,base_encryption, QModelIndex());
    baseTable->model->setData(index,"false");//设置初始值

    index = baseTable->model->index(baseTable->currentIndex().row()+1,base_compression, QModelIndex());
    baseTable->model->setData(index,"false");//设置初始值
}

void baseForm::on_delePushButton_clicked(){
    baseTable->model->removeRow(baseTable->currentIndex().row());
}


/***********表格初始化************/
void baseForm::tableInit(){
    QStringList items;
    items.append("true");
    items.append("false");

    QStringList items2;
    items2.append("tcp");
    items2.append("udp");

    baseTable->model->setHeaderData(base_sw,         Qt::Horizontal, QObject::tr("启用"));
    baseTable->model->setHeaderData(base_name,       Qt::Horizontal, QObject::tr("名称"));
    baseTable->model->setHeaderData(base_ip,         Qt::Horizontal, QObject::tr("转发IP"));
    baseTable->model->setHeaderData(base_type,       Qt::Horizontal, QObject::tr("协议"));
    baseTable->model->setHeaderData(base_local,      Qt::Horizontal, QObject::tr("本地端口"));
    baseTable->model->setHeaderData(base_remote,     Qt::Horizontal, QObject::tr("远程端口"));
    baseTable->model->setHeaderData(base_encryption, Qt::Horizontal, QObject::tr("加密"));
    baseTable->model->setHeaderData(base_compression,Qt::Horizontal, QObject::tr("压缩"));
    for(int col = 0; col < base_length; col++){
        QModelIndex index = baseTable->model->index(0, col, QModelIndex());
        if(col == base_sw){
            ComboDelegate *combo3 = new ComboDelegate;
            combo3->setItems(items);
            baseTable->setItemDelegateForColumn(col,combo3);
            baseTable->model->setData(index,"true");
        }
        if( col == base_name){
            baseTable->model->setData(index,"rd1");
        }
        if( col == base_ip){
            baseTable->model->setData(index,"127.0.0.1");
        }
        if(col == base_type){
            ComboDelegate *combo4 = new ComboDelegate;
            combo4->setItems(items2);
            baseTable->setItemDelegateForColumn(col,combo4);
            baseTable->model->setData(index,"tcp");
        }

        if( col == base_local){
            baseTable->model->setData(index,"3389");
        }

        if( col == base_remote){
            baseTable->model->setData(index,"7789");
        }
        if(col == base_encryption){
            ComboDelegate *combo1 = new ComboDelegate;
            combo1->setItems(items);
            baseTable->setItemDelegateForColumn(col,combo1);
            baseTable->model->setData(index,"false");
        }
        if(col == base_compression){
            ComboDelegate *combo2 = new ComboDelegate;
            combo2->setItems(items);
            baseTable->setItemDelegateForColumn(col,combo2);
            baseTable->model->setData(index,"false");
        }
    }
    /*列宽*/
    qint16 tableWidth =  ui->stackedWidget->geometry().width();
    qint16 Width = tableWidth / (base_length + 1) - 2;
    baseTable->setColumnWidth(base_sw,          Width);
    baseTable->setColumnWidth(base_name,        Width);
    baseTable->setColumnWidth(base_ip,          Width*2);
    baseTable->setColumnWidth(base_type,        Width);
    baseTable->setColumnWidth(base_local,       Width);
    baseTable->setColumnWidth(base_remote,      Width);
    baseTable->setColumnWidth(base_encryption,  Width);
    baseTable->setColumnWidth(base_compression, Width);
}


void baseForm::saveData(){
    settings->setValue("row",baseTable->model->rowCount());
    for( int i = 0; i < baseTable->model->rowCount() ; i++){
        for( int j = 0;j < base_length; j++ ){
            settings->setValue("row"+QString::number(i)+QString::number(j),baseTable-> GetItemData(i,j));
        }
    }
}
void baseForm::recData(){
    int oldRow = (settings->value("row")).toInt();
    int beginRow = baseTable->model->rowCount();
    if(oldRow > beginRow){
        for( int i = 0 ; i < (oldRow - beginRow) ; i++){
            baseTable->model->insertRow(beginRow);
        }
    }
    else {
        for( int i = 0 ; i < (beginRow - oldRow) ; i++){
            baseTable->model->insertRow(beginRow);
        }
    }
    for( int i = 0; i < baseTable->model->rowCount() ; i++){
        for( int j = 0;j < base_length; j++ ){
            QModelIndex index = baseTable->model->index(i, j, QModelIndex());
            baseTable->model->setData(index, (settings->value("row"+QString::number(i)+QString::number(j))).toString());
        }
    }
}

void baseForm::on_startButton_clicked(){
    mainw->on_startButton_clicked();
}

void baseForm::on_ResetButton_clicked(){
    mainw->on_ResetButton_clicked();
}

